﻿using System;
using System.Collections.Generic;
using Cashbook.Models;
using Cashbook.Services;
using Xamarin.Forms;

namespace Cashbook
{
    public partial class NewItemPage : ContentPage
    {
        public Bill Bill { get; set; }
		private BillsDataAccess dataAccess;

		public NewItemPage()
        {
          //  _db = db;
            InitializeComponent();
			this.dataAccess = new BillsDataAccess();

            Bill = new Bill
            {
				Title = "项目...",
                CustomerId = 0,
				Amount = 0,
				IsPayed = false,
				AddedDate = DateTime.Now
            };
           

            BindingContext = this;           
        }

        async void OnAddClick(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(titleEntry.Text)){
                await DisplayAlert("错误", "请输入项目主题","关闭");
                titleEntry.Focus();
                return;
            }
			if (string.IsNullOrEmpty(amountEntry.Text))
			{
				await DisplayAlert("错误", "请输入金额", "关闭");
				amountEntry.Focus();
				return;
			}
            int amount;           
            int.TryParse(amountEntry.Text,out amount);
            if (amount<=0)
			{
				await DisplayAlert("错误", "请输入金额必须大于0", "关闭");
				amountEntry.Focus();
				return;
			}

			if (string.IsNullOrEmpty(clientNameEntry.Text))
			{
				await DisplayAlert("错误", "请输入客户", "关闭");
				clientNameEntry.Focus();
				return;
			}

           

			//var bill = new Bill()
			//{
   //             Title = titleEntry.Text,
			//	AddedDate = DateTime.Now,
   //             Amount = Int32.Parse(amountEntry.Text),
   //             IsPayed = isPayedSwitch.IsToggled,
   //             ClientName = clientNameEntry.Text
			//};

            await  this.dataAccess.SaveBillAsync(Bill);
			
            //MessagingCenter.Send(this, "AddItem", bill);
            //await Navigation.PopToRootAsync();
            await Navigation.PushAsync(new ItemsPage());
        }
    }
}
